<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="车牌号" prop="vehicleNumber">
						<el-input v-model="form.vehicleNumber" placeholder="车牌号" clearable maxlength="20"></el-input>
					</el-form-item>
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号" clearable></el-input-number>
					</el-form-item>
					<el-form-item label="品牌" prop="brand">
						<el-input v-model="form.brand" placeholder="品牌" clearable maxlength="45"></el-input>
					</el-form-item>
					<el-form-item label="型号" prop="model">
						<el-input v-model="form.model" placeholder="型号" clearable maxlength="45"></el-input>
					</el-form-item>
					<el-form-item label="排量" prop="displacement">
						<el-input-number v-model="form.displacement" placeholder="排量" clearable></el-input-number>/L
					</el-form-item>
					<el-form-item label="发动机编号" prop="engineNo">
						<el-input v-model="form.engineNo" placeholder="发动机编号" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<sc-upload style="width: 100%;height: 280px"
						v-model="form.photo"
						:apiObj="$API.file.uploadImg"
						title="上传车辆照片"
					></sc-upload>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="颜色" prop="color">
						<el-color-picker v-model="form.color" :predefine="colorList">></el-color-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="座位数" prop="seats">
						<el-input-number v-model="form.seats" placeholder="座位数" clearable></el-input-number>/人
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="车架号" prop="frameNo">
						<el-input v-model="form.frameNo" placeholder="车架号" clearable maxlength="45"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="证书编号" prop="certification">
						<el-input v-model="form.certification" placeholder="证书编号" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="车辆性质" prop="nature">
						<sc-select v-model="form.nature" :params="{code:'vehicleNature'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="车辆类型" prop="type">
						<sc-select v-model="form.type" :params="{code:'vehicleType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="管理部门" prop="manageDept">
						<select-dept-dialog v-model:deptId="form.manageDept" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="managePhone">
						<el-input v-model="form.managePhone" placeholder="联系电话" clearable maxlength="20"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="车辆所有者" prop="onwer">
						<select-user-dialog v-model:accountId="form.onwer" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="onwerPhone">
						<el-input v-model="form.onwerPhone" placeholder="联系电话" clearable maxlength="20"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="长期使用者" prop="caruser">
						<select-user-dialog v-model:accountId="form.caruser" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="caruserPhone">
						<el-input v-model="form.caruserPhone" placeholder="联系电话" clearable maxlength="20"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="购买价格" prop="price">
						<el-input-number v-model="form.price" placeholder="购买价格" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="购置税" prop="tax">
						<el-input-number v-model="form.tax" placeholder="购置税" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="购买日期" prop="buyTime">
						<el-date-picker v-model="form.buyTime" placeholder="购买日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="起始里程" prop="mileage">
						<el-input-number v-model="form.mileage" placeholder="起始里程" clearable></el-input-number>/公量
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="年检日期" prop="yearlyTime">
						<el-date-picker v-model="form.yearlyTime" placeholder="年检日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="保险到期" prop="insureTime">
						<el-date-picker v-model="form.insureTime" placeholder="保险到期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="人员权限" prop="userRole">
				<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="部门权限">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="职务权限">
				<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'vehicle'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" placeholder="备注说明" clearable maxlength="200" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import AttachMenu from "@/views/module/attachMenu";
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu
	},
	data() {
		return {
			colorList: ['#000000','#FFFFFF',  '#536dfe', '#ff5c93', '#c62f2f', '#fd726d'],
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '创建车辆',
				edit: '编辑车辆',
				show: '查看'
			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			templates: [],
			form: {
				vehicleId: '',
				vehicleNumber: '',
				brand: '',
				sortNo: 0,
				photo:'',
				color: '#000000',
				model: '',
				displacement: '',
				engineNo: '',
				seats: 5,
				frameNo: '',
				certification: '',
				nature: '',
				type: '',
				manageDept: '',
				managePhone: '',
				onwer: '',
				onwerPhone: '',
				caruser: '',
				caruserPhone: '',
				price: 0,
				tax: '',
				buyTime: '',
				mileage: 0,
				yearlyTime: '',
				insureTime: '',
				userRole: '',
				deptRole: '',
				levelRole: '',
				remark: '',
				attachId: ''
			},
			rules: {
				vehicleNumber: [
					{required: true, message: '车牌号不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.vehicle.insertVehicleInfo.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.vehicle.updateVehicleInfo.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.vehicleId = data.vehicleId
			this.form.vehicleNumber = data.vehicleNumber
			this.form.photo = data.photo
			this.form.brand = data.brand
			this.form.sortNo = data.sortNo
			this.form.color = data.color
			this.form.model = data.model
			this.form.displacement = data.displacement
			this.form.engineNo = data.engineNo
			this.form.seats = data.seats
			this.form.frameNo = data.frameNo
			this.form.certification = data.certification
			this.form.nature = data.nature
			this.form.type = data.type
			this.form.manageDept = data.manageDept
			this.form.managePhone = data.managePhone
			this.form.onwer = data.onwer
			this.form.onwerPhone = data.onwerPhone
			this.form.caruser = data.caruser
			this.form.caruserPhone = data.caruserPhone
			this.form.price = data.price
			this.form.tax = data.tax
			this.form.buyTime = data.buyTime
			this.form.mileage = data.mileage
			this.form.yearlyTime = data.yearlyTime
			this.form.insureTime = data.insureTime
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.remark = data.remark
			this.form.attachId = data.attachId
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
